<?php

/**
 * resource actions.
 *
 * @package    mysmartmanager
 * @subpackage resource
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class resourceActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->resources = Doctrine::getTable('Resource')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    //$request->checkCSRFProtection();
	try
	{
	  $loggedInResourceId = sfContext::getInstance()->getUser()->getGuardUser()->getResource()->getResourceid();
	  if ($loggedInResourceId)
	    $this->resource = Doctrine::getTable('Resource')->find(array($loggedInResourceId));
	  else 
	    $this->redirect('resource/new');
	}
	catch(sfException $e)
	{
	  $this->redirect('resource/new');  
	}
	
    
	$requestedResourceId = $this->resource->getResourceid();
	sfContext::getInstance()->getUser()->setAttribute('requestedResourceId', $requestedResourceId);
	if ($requestedResourceId <> $loggedInResourceId)
	{
	  //redirect some where
	  //echo "kider pa ji???";
	}
	//$this->resource = Doctrine::getTable('Resource')->find(array($request->getParameter('resourceid')));
    //$this->forward404Unless($this->resource);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ResourceForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ResourceForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($resource = Doctrine::getTable('Resource')->find(array($request->getParameter('resourceid'))), sprintf('Object resource does not exist (%s).', $request->getParameter('resourceid')));
    $this->form = new ResourceForm($resource);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($resource = Doctrine::getTable('Resource')->find(array($request->getParameter('resourceid'))), sprintf('Object resource does not exist (%s).', $request->getParameter('resourceid')));
    $this->form = new ResourceForm($resource);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($resource = Doctrine::getTable('Resource')->find(array($request->getParameter('resourceid'))), sprintf('Object resource does not exist (%s).', $request->getParameter('resourceid')));
    $resource->delete();

    $this->redirect('resource/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $resource = $form->save();
      $this->getUser()->setFlash('notice', 'Profile Updated Successfully.');
	  
      $this->redirect($this->generateUrl('resource_show', $resource));
    }
  }
  
  private function getResouces()
  {
    $this->resources = Doctrine::getTable('Resource')
      ->createQuery('a')
      ->where('a.resourceid != ?', $this->getUser()->getResourceId())
      ->execute();
	return $this->resources;
  }
  public function executeViewcost(sfWebRequest $request)
  {
    $this->resources = $this->getResouces();
  }    
  public function executeAssigncost(sfWebRequest $request)
  {
    $this->resources = $this->getResouces();
  }
  public function executeUpdatecost(sfWebRequest $request)
  {    
     $ids = $request->getParameter('ids');
	 $rates = $request->getParameter('rates'); 
	 	 
	 if (!$ids)
     {
       $this->getUser()->setFlash('error', 'You must at least select one item.');
       $this->redirect('resource/assigncost');
     }
	 /*
	 $records = Doctrine_Query::create()
      ->from('resource')
      ->whereIn('id', $ids)
      ->execute();

    foreach ($records as $record)
    {
      $record-> set("rate", $request->getParameter($resource->getResourceid()));
	  $record->save();
    }*/

	for ($index=0; $index < count($ids); $index++) {
      $rid = $ids[$index];	  
	  $resource = Doctrine::getTable('Resource')->find(array($rid));
	  $resource -> set("rate", $rates[$index]);
	  $resource -> save();
	}    
	$this->getUser()->setFlash('notice', 'Cost of selected resource(s) updated successfully.');
    $this->redirect('resource/viewcost');
	  
  }
}
